<?php
/**
 * Notes:
 * User: wudg <544038230@qq.com>
 * Date: 2025/3/31 11:37
 */

namespace App\Exception\Handler;


use App\Traits\ApiResponseTrait;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\Validation\ValidationException;
use Psr\Http\Message\ResponseInterface;
use Throwable;
class ValidatorExceptionHandler  extends ExceptionHandler
{
    use ApiResponseTrait;

    public function handle(Throwable $throwable, ResponseInterface $response)
    {

        if($throwable instanceof ValidationException)
        {
            $this->stopPropagation(); //阻止冒泡，不再向下继续
            $errorList = $throwable->errors();
            $errorMsg = $errorList[array_key_first($errorList)][0] ?? '参数验证失败';
            return $this->setHttpCode(200)->fail($errorMsg);
        }

        return $response;
    }

    public function isValid(Throwable $throwable): bool
    {
        return true;
    }
}